package com.bai.ad;

import com.alibaba.fastjson.JSON;
import com.bai.ad.client.SponsorClient;
import com.bai.ad.client.vo.AdPlanGetRequest;
import com.bao.ad.annotation.IgnoreResponseAdvice;
import com.bao.ad.vo.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * ClassName SearchController
 *
 * @author Bai
 * @version 1.0
 * @date 2019/2/24 0024 下午 18:58
 **/
@Slf4j
@RestController
public class SearchController {

    private final RestTemplate restTemplate;

    private final SponsorClient sponsorClient;

    @Autowired
    public SearchController(RestTemplate restTemplate, SponsorClient sponsorClient) {
        this.restTemplate = restTemplate;
        this.sponsorClient = sponsorClient;
    }

    @IgnoreResponseAdvice
    @PostMapping(value = "/getAdPlans")
    public CommonResponse getAdPlans(AdPlanGetRequest request){
        log.info("ad-search: getAdPlans -> {}", JSON.toJSONString(request));
        return sponsorClient.getAdPlans(request);
    }

    @IgnoreResponseAdvice
    @PostMapping("/getAdPlansByRebbon")
    public CommonResponse getAdPlansByRebbon(
            AdPlanGetRequest request){
        log.info("ad-search: getAdPlansByRibbon -> {}", JSON.toJSONString(request));
        return restTemplate.postForEntity(
                "http://eureka-client-ad-sponser/ad-sponsor/get/adPlan",
                request,
                CommonResponse.class
        ).getBody();
    }

    @IgnoreResponseAdvice
    @GetMapping("/info")
    public CommonResponse getInfo(){
        log.info("ad-search: getInfo()");
        return restTemplate.postForEntity(
                "http://eureka-client-ad-sponser/ad-sponsor/info", null, CommonResponse.class
        ).getBody();
    }
}
